from gi.repository import Unity, Gio, GObject, Dbusmenu
import sys
import thread
import time
import argparse
from threading import Thread


class MyThread(Thread):
	def __init__ (self):
		Thread.__init__(self)
		self.loop = loop = GObject.MainLoop()	
	def run(self):
		GObject.threads_init()
		self.loop.run()
		#while 1: print "hello from another therad"

def do(): print "hello!"
if __name__ == '__main__':
	"""parser = argparse.ArgumentParser(sys.stdin)

	parser.add_argument('appname', type=str, help='application name, ends with .desktop')
	parser.add_argument('method', type=str, help='application name, ends with .desktop')
	parser.add_argument('property', type=str, nargs="?", help='get property')
	parser.add_argument('-count', type=int, help='number on the launcher')
	parser.add_argument('-count-visible', type=bool, help='number on the launcher')
	parser.add_argument('-progress', type=int, help='number on the launcher')
	parser.add_argument('-progress-visible', type=bool, help='number on the launcher')
	parser.add_argument('-urgent', type=bool, help='number on the launcher')

	args = parser.parse_args();

	launcher = Unity.LauncherEntry.get_for_desktop_id(args.appname)
	
	if(args.method == "get"):
		print(str(launcher.get_property(args.property)))
	elif(args.method == "set"):
		launcher.set_property()"""
	appname = sys.argv[0]
	if(appname):
		print 0
		launcher = Unity.LauncherEntry.get_for_desktop_id(appname)
		thread = MyThread()
		thread.start()
		#while 1: print "just hello"
		print 'started'
		while 1:
			try:
				line = sys.stdin.readline().strip()
				args = line.split(' ')
				if(args[0] == 'get'):
					value = launcher.get_property(args[1])
					print value
					#elif(args[1] == 'progress':
					#elif(args[1] == 'count':
					#else:
				elif(args[0] == 'set'):
					launcher.set_property(args[1],args[2])
					print 0	
				else:
					print 1;
			except Exception:
				print 1
				
	else:
		print 1
	'''loop = GObject.MainLoop()
 
	 
	GObject.timeout_add_seconds(5, do)
	loop.run()'''



